﻿using System.Collections.Generic;
using System.Windows.Media;
using System.Windows;
using System.ComponentModel;
using System.Windows.Shapes;

namespace Utils.Wpf.Chart
{
    public class ChartSerie : FrameworkElement, INotifyPropertyChanged
    {
        #region Dependence Properties

        public static DependencyProperty BrushProperty = DependencyProperty.Register("Brush", typeof(Brush), typeof(ChartSerie), new FrameworkPropertyMetadata(OnBrushChanged));
		public static DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(ReadWriteLockedObservableCollection<ChartPoint>), typeof(ChartSerie), new FrameworkPropertyMetadata(OnDataChanged));
		public static DependencyProperty ThicknessProperty = DependencyProperty.Register("Thickness", typeof(int), typeof(ChartSerie), new FrameworkPropertyMetadata(OnThicknessChanged));
		public static DependencyProperty DrawPointsProperty = DependencyProperty.Register("DrawPoints", typeof(bool), typeof(ChartSerie), new FrameworkPropertyMetadata(OnDrawPointsChanged));
        public new static DependencyProperty VisibilityProperty = DependencyProperty.Register("Visibility", typeof(Visibility), typeof(ChartSerie), new FrameworkPropertyMetadata(OnVisibilityChanged));
        public static DependencyProperty AxisIndexProperty = DependencyProperty.Register("AxisIndex", typeof(int), typeof(ChartSerie), new FrameworkPropertyMetadata(OnAxisIndexChanged));

        #endregion

        #region Properties

        public Polyline Line { get; private set; }
        public List<Rectangle> LinePoints { get; private set; }

		public ReadWriteLockedObservableCollection<ChartPoint> Data 
        {
            get
            {
				return (ReadWriteLockedObservableCollection<ChartPoint>)GetValue(DataProperty);
            }
            set
            {
                SetValue(DataProperty, value);
            }
        }

        public Brush Brush 
        {
            get
            {
                return (Brush)GetValue(BrushProperty);
            }
            set
            {
                SetValue(BrushProperty,value);
            }
        }

    	public int Thickness
    	{
			get
			{
				return (int)GetValue(ThicknessProperty);
			}
			set
			{
				SetValue(ThicknessProperty, value);
			}    		
    	}

		public bool DrawPoints
		{
			get
			{
				return (bool)GetValue(DrawPointsProperty);
			}
			set
			{
				SetValue(DrawPointsProperty, value);
			}
		}

        public new Visibility Visibility
        {
            get
            {
                return (Visibility)GetValue(VisibilityProperty);
            }
            set
            {
                SetValue(VisibilityProperty, value);
            }
        }

        public int AxisIndex
        {
            get
            {
                return (int)GetValue(AxisIndexProperty);
            }
            set
            {
                SetValue(AxisIndexProperty, value);
            }
        }

        #endregion 

        #region Public Methods

        public ChartSerie()
        {
            this.Brush = Brushes.White;
            this.Line = new Polyline();
            this.LinePoints = new List<Rectangle>();
        }

		public void Clear()
		{
			this.LinePoints.Clear();
			this.Line.Points.Clear();	
		}

        #endregion

        #region Event Handlers

        private static void OnBrushChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            ChartSerie serie = (ChartSerie)source;
            serie.OnPropertyChanged("Brush");
        }

        private static void OnDataChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            ChartSerie serie = (ChartSerie)source;
            serie.OnPropertyChanged("Data");
        }

		private static void OnThicknessChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
		{
			ChartSerie serie = (ChartSerie)source;
			serie.OnPropertyChanged("Thickness");
		}

		private static void OnDrawPointsChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
		{
			ChartSerie serie = (ChartSerie)source;
			serie.OnPropertyChanged("DrawPoints");
		}

        private static void OnVisibilityChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
		{
			ChartSerie serie = (ChartSerie)source;
			serie.OnPropertyChanged("Visibility");
		}

        private static void OnAxisIndexChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
		{
			ChartSerie serie = (ChartSerie)source;
            serie.OnPropertyChanged("AxisIndex");
		}
        
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
        }

        #endregion
    }
}
